package com.zxmc;

import com.alibaba.fastjson2.JSONObject;
import com.zxmc.pojo.dto.NovelDto;
import com.zxmc.pojo.entity.Novel;
import com.zxmc.server.service.NovelService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


@SpringBootTest
@Component
//@MapperScan("mapper")
public class PythonUpdateCover {
    @Autowired
    private NovelService novelService;
    @Test
    public void test() {
        String[] urls={
                "https://fanqienovel.com/page/7276384138653862966",
                "https://fanqienovel.com/page/7143038691944959011",
                "https://fanqienovel.com/page/6982529841564224526",
                "https://fanqienovel.com/page/7400669579342531646",
                "https://fanqienovel.com/page/7284260705774078987",
                "https://fanqienovel.com/page/7256784068786785336",
                "https://fanqienovel.com/page/7356936233148222488",
                "https://fanqienovel.com/page/7307496815333477438",
                "https://fanqienovel.com/page/7290241671466322984",
                "https://fanqienovel.com/page/7110130017044007967",
                "https://fanqienovel.com/page/7383825464940317720",
                "https://fanqienovel.com/page/7069983601739172895",
                "https://fanqienovel.com/page/7391663420275182617",
                "https://fanqienovel.com/page/7229199097683512381",
                "https://fanqienovel.com/page/7381642774535212094",
                "https://fanqienovel.com/page/7197237867460955196",
                "https://fanqienovel.com/page/7091545438385343522",
                "https://fanqienovel.com/page/7402200659753176126",
                "https://fanqienovel.com/page/7297873858458029107",
                "https://fanqienovel.com/page/7153918048745819172",
                "https://fanqienovel.com/page/7261739716398025762",
                "https://fanqienovel.com/page/6569997839437401101",
                "https://fanqienovel.com/page/6909448739476212749",
                "https://fanqienovel.com/page/7342400869209492542",
                "https://fanqienovel.com/page/7444482186822044734",
                "https://fanqienovel.com/page/7375572126402612286",
                "https://fanqienovel.com/page/7138249318909086727",
                "https://fanqienovel.com/page/7338034838672247833",
                "https://fanqienovel.com/page/7421167583522458648",
                "https://fanqienovel.com/page/7429537438936075289",
                "https://fanqienovel.com/page/7297316760233970688",
                "https://fanqienovel.com/page/7442050065109109822",
                "https://fanqienovel.com/page/6838480082219043843",
                "https://fanqienovel.com/page/7297316760233970688",
                "https://fanqienovel.com/page/7214945403312016440",
                "https://fanqienovel.com/page/7197237867460955196",
                "https://fanqienovel.com/page/7353199267630025753",
                "https://fanqienovel.com/page/7287479909398613032",
                "https://fanqienovel.com/page/7103712960068979712",
                "https://fanqienovel.com/page/7324876104668302398",
                "https://fanqienovel.com/page/7091545438385343522",
                "https://fanqienovel.com/page/7038385168364604451",
                "https://fanqienovel.com/page/6993228831532256267",
                "https://fanqienovel.com/page/6837522594468465678",
                "https://fanqienovel.com/page/7281495609549261881",
                "https://fanqienovel.com/page/7198789276006353981",
                "https://fanqienovel.com/page/7352919138655816766",
                "https://fanqienovel.com/page/6909448739476212749",
                "https://fanqienovel.com/page/7404634638590675993",
                "https://fanqienovel.com/page/7046842344074644484",
                "https://fanqienovel.com/page/7094544464168881156",
                "https://fanqienovel.com/page/6946068119479651336",
                "https://fanqienovel.com/page/7347228964194110489",
                "https://fanqienovel.com/page/7261895220290128908",
                "https://fanqienovel.com/page/6924399043925773325",
                "https://fanqienovel.com/page/7288502462875438138",
                "https://fanqienovel.com/page/7113743406903331881",
                "https://fanqienovel.com/page/7287329633438010387",
                "https://fanqienovel.com/page/7180330734979124256",
                "https://fanqienovel.com/page/6589909026790706180",
                "https://fanqienovel.com/page/7143506226230332447",
                "https://fanqienovel.com/page/7087519624329169951",
                "https://fanqienovel.com/page/7231622035749735483",
                "https://fanqienovel.com/page/7091211048631405568",
                "https://fanqienovel.com/page/7207346568079346746",
                "https://fanqienovel.com/page/7024903102301998111",
                "https://fanqienovel.com/page/7281543433682947109",
                "https://fanqienovel.com/page/7137160952171990055",
                "https://fanqienovel.com/page/7227792566651980812",
                "https://fanqienovel.com/page/7415496563960581144",
                "https://fanqienovel.com/page/6976895632640314375",
                "https://fanqienovel.com/page/7350264177715334169",
                "https://fanqienovel.com/page/7294284291129740322",
                "https://fanqienovel.com/page/7356605623657581630",
                "https://fanqienovel.com/page/7374301193213856793",
                "https://fanqienovel.com/page/7099413677245205543",
                "https://fanqienovel.com/page/7424812975858404377",
                "https://fanqienovel.com/page/7346518344150830105",
                "https://fanqienovel.com/page/7238534936678894626",
                "https://fanqienovel.com/page/7412901630074440729",
                "https://fanqienovel.com/page/6942441629332540423",
                "https://fanqienovel.com/page/7028515360076729378",
                "https://fanqienovel.com/page/7224364663184559143",
                "https://fanqienovel.com/page/7250432542971530243",
                "https://fanqienovel.com/page/7280302140692630528",
                "https://fanqienovel.com/page/7324242366959733822",
                "https://fanqienovel.com/page/7308676492311923774",
                "https://fanqienovel.com/page/7184812049967352844",
                "https://fanqienovel.com/page/7211814669936233507",
                "https://fanqienovel.com/page/7083672225286458406",
                "https://fanqienovel.com/page/7207066781708454916",
                "https://fanqienovel.com/page/6900994666632580109",
                "https://fanqienovel.com/page/7403249804093951001",
                "https://fanqienovel.com/page/7197730509596658749",
                "https://fanqienovel.com/page/7254059928380247078",
                "https://fanqienovel.com/page/7448938859208903742",
                "https://fanqienovel.com/page/7075375974107319300",
                "https://fanqienovel.com/page/7366870956771380248",
                "https://fanqienovel.com/page/6980340342377679886",
                "https://fanqienovel.com/page/7307976828990278718",
                "https://fanqienovel.com/page/7350520004426599448",
                "https://fanqienovel.com/page/7229199097683512381",
                "https://fanqienovel.com/page/7329711628327341118",
                "https://fanqienovel.com/page/7386056175252409369",
                "https://fanqienovel.com/page/7320653712605596734",
                "https://fanqienovel.com/page/7229602235896826932",
                "https://fanqienovel.com/page/7035843871657430023",
                "https://fanqienovel.com/page/7195109011966921762",
                "https://fanqienovel.com/page/7280093652901170211",
                "https://fanqienovel.com/page/7308395441471622184",
                "https://fanqienovel.com/page/7259570264529177636",
                "https://fanqienovel.com/page/7278254291855346728",
                "https://fanqienovel.com/page/7188710224214101052",
                "https://fanqienovel.com/page/7359545185606306841",
                "https://fanqienovel.com/page/7127488556896881699",
                "https://fanqienovel.com/page/7348067298554416190",
                "https://fanqienovel.com/page/7220463153157508154",
                "https://fanqienovel.com/page/6995119379645991944",
                "https://fanqienovel.com/page/7428889655749135385",
                "https://fanqienovel.com/page/7035494730368551951",
                "https://fanqienovel.com/page/7026630823847660547",
                "https://fanqienovel.com/page/7317525935996013630",
                "https://fanqienovel.com/page/7267044658339908649",
                "https://fanqienovel.com/page/7241158822671158306",
                "https://fanqienovel.com/page/7354873471911873561",
                "https://fanqienovel.com/page/7098621399203318824",
                "https://fanqienovel.com/page/7352208123316145214",
                "https://fanqienovel.com/page/7404711634653940760",
                "https://fanqienovel.com/page/7377364383015373849",
                "https://fanqienovel.com/page/7297844180821216318",
                "https://fanqienovel.com/page/7250791390399958053",
                "https://fanqienovel.com/page/7415612961999293502",
                "https://fanqienovel.com/page/7340539547924581401",
                "https://fanqienovel.com/page/7355744477631220798",
                "https://fanqienovel.com/page/7172581783488171021",
                "https://fanqienovel.com/page/7427261116889189438",
                "https://fanqienovel.com/page/7308587104030690343",
                "https://fanqienovel.com/page/7233032339511577604",
                "https://fanqienovel.com/page/7103353089607863296",
                "https://fanqienovel.com/page/7321950014144515134",
                "https://fanqienovel.com/page/7104916285464513566",
                "https://fanqienovel.com/page/7267451340366810127",
                "https://fanqienovel.com/page/7121505991786499102",
                "https://fanqienovel.com/page/6877775273685683214",
                "https://fanqienovel.com/page/7435382525997878296",
                "https://fanqienovel.com/page/7238266881507855394",
                "https://fanqienovel.com/page/7317402128576695358",
                "https://fanqienovel.com/page/7341380231623609369",
                "https://fanqienovel.com/page/7385856065935510590",
                "https://fanqienovel.com/page/7239360051335400487",
                "https://fanqienovel.com/page/7366888666385353790",
                "https://fanqienovel.com/page/7085357205842562078",
                "https://fanqienovel.com/page/7288244619387604031",
                "https://fanqienovel.com/page/7005106440989838336",
                "https://fanqienovel.com/page/7290379815469714489",
                "https://fanqienovel.com/page/7173549484771642406",
                "https://fanqienovel.com/page/7281437974535867455",
                "https://fanqienovel.com/page/7305338449282731020",
                "https://fanqienovel.com/page/7371068470953249854",
                "https://fanqienovel.com/page/7368083003844594713",
                "https://fanqienovel.com/page/7366881285945445400",
                "https://fanqienovel.com/page/7355543162028313625",
                "https://fanqienovel.com/page/7426384401644129305",
                "https://fanqienovel.com/page/7273721021448850494",
                "https://fanqienovel.com/page/7232168369636510772",
                "https://fanqienovel.com/page/7020314654467427341",
                "https://fanqienovel.com/page/7379940771354397720",
                "https://fanqienovel.com/page/7282046097365666875",
                "https://fanqienovel.com/page/7411976522405268542",
                "https://fanqienovel.com/page/7263489983887969315",
                "https://fanqienovel.com/page/7327874211144289342",
                "https://fanqienovel.com/page/7212906786754399232",
                "https://fanqienovel.com/page/7419976264527662104",
                "https://fanqienovel.com/page/7280118553863261220",
                "https://fanqienovel.com/page/6995009818402491426",
                "https://fanqienovel.com/page/7244459054397918246",
                "https://fanqienovel.com/page/7361342003763743806",
                "https://fanqienovel.com/page/7342140599404874776",
                "https://fanqienovel.com/page/7239690868863536167",
                "https://fanqienovel.com/page/7087043060978158599",
                "https://fanqienovel.com/page/7433611025347267608",
                "https://fanqienovel.com/page/7278676011321936958",
                "https://fanqienovel.com/page/7218125914356321320",
                "https://fanqienovel.com/page/7311119765634829374",
                "https://fanqienovel.com/page/6967546188178394148",
                "https://fanqienovel.com/page/7374012235795926078",
                "https://fanqienovel.com/page/6826473708392025101",
                "https://fanqienovel.com/page/7262709887350606887",
                "https://fanqienovel.com/page/7165749394245946405",
                "https://fanqienovel.com/page/7290202917510843411",
                "https://fanqienovel.com/page/7222948784999762947",
                "https://fanqienovel.com/page/6955825218392689678",
                "https://fanqienovel.com/page/7350519177519582232",
                "https://fanqienovel.com/page/7028759829283867652",
                "https://fanqienovel.com/page/7388428216165223486",
                "https://fanqienovel.com/page/7363925228830854206",
                "https://fanqienovel.com/page/7376475721025195070",
                "https://fanqienovel.com/page/7144933328859171848",
                "https://fanqienovel.com/page/7268213153660406840",
                "https://fanqienovel.com/page/7064102811843169315",

        };
        for (String url : urls) {
            try {
                System.out.println(url);
                // 创建一个ProcessBuilder对象，指定要执行的Python脚本和参数
                ProcessBuilder pb = new ProcessBuilder("python", "F:\\桌面\\bishe\\bishe_back\\bishe_back\\src\\main\\resources\\static\\crawerCover.py",url);

                // 启动进程
                Process process = pb.start();

                // 获取进程的输出流
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                // 读取Python脚本的输出
                StringBuilder output = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    output.append(line);
                }

                // 等待进程结束
                int exitCode = process.waitFor();
                System.out.println("Python脚本执行完毕，退出码: " + exitCode);
//                System.out.println(output);
//             将Python脚本的输出解析为JSON对象
                NovelDto novelDto =JSONObject.parseObject(output.toString(), NovelDto.class);

                // 打印JSON对象
                System.out.println(novelDto);
                Novel novel = new Novel();
                BeanUtils.copyProperties(novelDto,novel);
                if(novel.getTitle()==null){
                    continue;
                }
                //调用
                try {
                    novelService.updateCover(novel);
//                    novelService.insert(novelDto);
                } catch (DuplicateKeyException e) {
                    System.out.println("重复小说");
                }
                Thread.sleep(1000);

            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }
}
